package com.huafv.flyauth.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huafv.flyauth.web.admin.entity.SysRoleMenu;
import com.huafv.flyauth.web.admin.mapper.SysRoleMenuMapper;
import com.huafv.flyauth.web.admin.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    @Transactional
    public boolean updateRoleMenus(Long roleId, List<SysRoleMenu> roleMenus) {
        // 删除该角色原有的权限记录
        this.baseMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));

        // 保存新的角色权限记录
        if (!CollectionUtils.isEmpty(roleMenus)) {
            for (SysRoleMenu roleMenu : roleMenus) {
                roleMenu.setRoleId(roleId);
                this.baseMapper.insert(roleMenu);
            }
        }

        return true;
    }
}
